#include "dbtabbase.h"
#include "databasequery.h"

#include "dbtabbase.h"
#include "databasequery.h"
#include "dbtabtabversion.h"
#include "dbtabtabversion.h"
//#include "dbtabformulainfo.h"

#include <QDebug>

const char* DBTabBase::_ID = ("fid");



DBTabBase::DBTabBase(const QLatin1String &name,QObject *obj):QObject(obj),mTabName(name)
{
}

QStringList DBTabBase::getColumnNameList(const QString &tabName,QSqlDatabase &db)
{
    DataBaseQuery query(db);
    QStringList list;
    if(query.exec("PRAGMA table_info("+ tabName+ ")"))
    {
        int index = query.record().indexOf("name");
        while(query.next()){
            list.append(query.value(index).toString());
        }
    }
    return list;
}

void DBTabBase::createTab(const QLatin1String &name,const DBTabBase::ColumnInfo infoTab[], QSqlDatabase &db)
{qDebug()<<__LINE__;
    DataBaseQuery query2(db,false);
    query2.deleteTable(name);qDebug()<<__LINE__;
    
    DataBaseQuery query(db);
    const ColumnInfo *info = &infoTab[0];
    
    QString str= "CREATE TABLE "+name +"(";
    while(info->columnName!=nullptr){
        str += "\n"+QString(info->columnName)+" "+ info->keyString +",";
        info++;
    }
//    if(QString::compare(name,QString(DBTabFormulaInfo::TAB_NAME))==0)
//    {
//        str +="UNIQUE(" + QString(DBTabFormulaInfo::_FORMULA_NAME)+","+QString(DBTabFormulaInfo::_FORMULA_MATERIAL)+")";
//    }
//    else
        str.chop(1);
    str += ")\n";qDebug()<<__LINE__;
    if(query.exec(str)==false)
    {
         qDebug()<<" create " +  name + " error \n"<<str;
    }
}

const QLatin1String &DBTabBase::tabName()
{
    return mTabName;
}

int DBTabBase::getVersion(QSqlDatabase &db)
{
    return DBTabTabVersion::getVersion(mTabName,db);
}

QStringList DBTabBase::getColumnNameList(QSqlDatabase &db)
{
    DataBaseQuery query(db);
    QStringList list;
    if(query.exec("PRAGMA table_info("+ mTabName+ ")"))
    {
        int index = query.record().indexOf("name");
        while(query.next()){
            list.append(query.value(index).toString());
        }
    }
    return list;
}

bool DBTabBase::syncSelect(DataBaseQuery &query)
{
    return query.selectTable(mTabName);
}

QString DBTabBase::queryData2String(const DataBaseQuery &query)
{
    QString str;
    QSqlRecord rec = query.record();
    int counts = rec.count();
    for(int i=0,t=counts-1; i<t; i++)
    {
        str.append(rec.value(i).toString());
        str.append(":");
    }
    str.append(rec.value(counts-1).toString());
    return str;
}

void DBTabBase::insertRow(const QStringList &line){
    QStringList list = getColumnNameList();
    int t = list.size();
    if(t==line.size()){
        QMap<QString,QVariant> map;
        for(int i=0;i<t;i++){
            map.insert(list.at(i),line.at(i));
        }
        DataBaseQuery query;
        query.insert(mTabName,map);
    }else{
        qWarning("please re-implement insert row for %s",mTabName.latin1());
    }
}

void DBTabBase::setVersion(int ver,QSqlDatabase &db)
{
    DBTabTabVersion::getDBTab()->setVersion(mTabName,ver,db);
    emit db_updated();
}

bool DBTabBase::isExist(const QString &tabName, const QString &fieldName, const QString &name)
{
    DataBaseQuery query = DataBaseQuery();
    QStringList list;
    list<<fieldName;

    if(query.selectTable(tabName,
                      &list,
                      fieldName+"=\""+name.trimmed()+"\""))
    {
        if(query.first()){
            return true;
        }
    }

    return false;
}


